package com.atguigu.ssyx.acl.service.impl;

import com.atguigu.ssyx.acl.mapper.RoleMapper;
import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.RolePermissionService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.model.acl.RolePermission;
import com.atguigu.ssyx.vo.acl.RoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

import java.util.stream.Collectors;

/**
 * @作者:胡树苑
 * @时间:2025/9/10 20:18
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    //用户角色关系
    @Autowired
    private AdminRoleService adminRoleService;
    //角色权限关系
    @Autowired
    private RolePermissionService rolePermissionService;

    @Override
    public IPage<Role> selectRolePage(Page<Role> pageParam, RoleQueryVo roleQueryVo) {
        //获取条件值
        String roleName = roleQueryVo.getRoleName();
        //创建mp条件对象
        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        //判断条件值是否为空，不为空封装查询条件
        if(!StringUtils.isEmpty(roleName)){
            wrapper.like(Role::getRoleName, roleName);
        }
        //调用方法实现条件分页查询
        Page<Role> rolePage = baseMapper.selectPage(pageParam, wrapper);
        //返回分页对象
        return rolePage;
    }

    @Override
    public Map<String, Object> getRoleByAdminId(Long adminId) {
        //查询所有的角色
        List<Role> allRolesList = baseMapper.selectList(null);

        //根据用户id查询用户分配的角色列表
        //1.根据用户id查询用户角色关系表admin_role查询用户分配角色id列表  List<AdminRole>
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        //设置查询条件,根据用户id adminid
        wrapper.eq(AdminRole::getAdminId, adminId);
        List<AdminRole> adminRoleList = adminRoleService.list(wrapper);
        //2.通过第一步返回的集合，获取所有角色id的列表List<Long>
        // 新建一个存放roleId的列表
        List<Long> roleIdsList = new ArrayList<>();
       // 遍历adminRoleList中的每个元素
        for (AdminRole item : adminRoleList) {
            // 从每个元素中获取roleId，添加到新列表中
            Long roleId = item.getRoleId();
            roleIdsList.add(roleId);
        }
        //3.创建新的list集合，用于存储用户配置的角色
        List<Role> assignRoleList = new ArrayList<>();
        //4.遍历所有的角色allRolesList得到每一个角色
        //判断所有的角色里面是否包含已经分配角色id,封装到3里面新的list集合
        for (Role role : allRolesList) {
            if(roleIdsList.contains(role.getId())){
                assignRoleList.add(role);
            }
        }
        //封装map返回
        Map<String, Object> result = new HashMap<>();
        result.put("allRolesList", allRolesList);
        result.put("assignRoles", assignRoleList);
        return result;
    }
    //为用户分配角色
    @Override
    public void saveAdminRole(Long adminId, Long[] roleIds) {
        //1.删除用户已经分配过的角色数据
        //根据用户id删除admin_role表里面对应的数据
        LambdaQueryWrapper<AdminRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminRole::getAdminId, adminId);
        adminRoleService.remove(wrapper);
        //重新分配
        //遍历多个角色id，得到每一个角色id，拿着每一个角色id+用户id添加到admin_role中
        List<AdminRole> adminRoleList = new ArrayList<>();
        for (Long roleId : roleIds) {
            AdminRole adminRole = new AdminRole();
            adminRole.setAdminId(adminId);
            adminRole.setRoleId(roleId);
            //放到list集合
            adminRoleList.add(adminRole);
        }
       //屌用方法添加
        adminRoleService.saveBatch(adminRoleList);
    }
    //为角色分配权限
    @Override
    public void savePermissionRole(Long roleId, Long[] permissionIds) {
        LambdaQueryWrapper<RolePermission> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RolePermission::getRoleId, roleId);
        rolePermissionService.remove(wrapper);
        List<RolePermission> rolePermissionList = new ArrayList<>();
        for (Long permissionId : permissionIds) {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            rolePermissionList.add(rolePermission);
        }
        rolePermissionService.saveBatch(rolePermissionList);
    }
}
